﻿/*
*JS Cart - XDeal
*Cuong Hung - 2011.11.23
*/

$(function () {
    var jsCart = new Cart();
    jsCart.InitCartFunctions();
});

function Cart() {
    //===Private Functions===

    var ControlNumberDeal = function () {
        $(".title-soluong input:text").keyup(function () {
            var parentTag = $(this).parent().parent().parent();
            var newprice = parentTag.find("#price").val() * $(this).val();
            parentTag.find(".title-thanhtien").html(formatCurrency(newprice));
            var totalSSS = +$("#shipping").val();
            $(".sum_item_value_price").html("<p>" + formatCurrency(TotalSumPrice()) + "</p>");
            var totalPriceAndShip = TotalSumPrice() + totalSSS;
            $(".sum_item_value_total").html("<p>" + formatCurrency(totalPriceAndShip) + "</p>");
        });
        $(".title-soluong input:text").focus(function(){
            $(this).select();
        });
    };

    var ControlCartChange = function () {
        //$(".title-soluong input:text").
    }
    

    var TotalSumPrice = function () {
        var totalPrice = 0;
        $(".cart_item").each(function () {
            totalPrice += $(this).find("#price").val() * $(this).find("#tbxQuantity").val();
        });
        return  totalPrice;
    };


    var DeleteItemInCart = function () {
        $(".title-del a").click(function () {

            var r = confirm("Xóa sản phẩm vừa chọn ?")
            if (r == true) {
                var thisparent = $(this).parent().parent().parent();
                DeleteServerCart($(thisparent).find("#hdDealIfoID").val());
                $(thisparent).remove();
                UpdateUserCart();
                UpdateServerCart();
            }
            else {

            }

        });
    }

    var UpdateUserCart = function () {
        var totalprice = 0;

        var totalSSS = +$("#shipping").val();
        $(".sum_item_value_price").html("<p>" + formatCurrency(TotalSumPrice()) + "</p>");
        var totalPriceAndShip = TotalSumPrice() + totalSSS;
        $(".sum_item_value_total").html("<p>" + formatCurrency(totalPriceAndShip) + "</p>");
    }
    var DeleteServerCart = function (deleteID) {

        $.ajax({
            type: "POST",
            url: '../../GioHang/DeleteItemInCart/',
            data: { deleteDealID: deleteID },
            success: function (msg) {
                if (parseInt(msg) == 1)
                { } else { }
            }
        });
    }//end delete server cart

    var UpdateServerCart = function () {

        $(".cart_item").each(function () {
            UpdateCartItem($(this).find("#hdDealIfoID").val(), $(this).find("#tbxQuantity").val());
        });
    }
    var UpdateCartItem = function (dealup, number) {
        
        $.ajax({
            type: "POST",
            url: '../../GioHang/UpdateCartItem/',
            data: { dealidupdate: dealup, quantity: number },
            success: function (msg) {
                
            }
        }); //end ajax function
    }


    //===Public Functions===
    return {
        InitCartFunctions: function () {
            ControlNumberDeal();
            DeleteItemInCart();
            
        }
    };
}



function blockNotNumber(e) {
    var e = window.event || e;
    if (window.event) {
        if (e.keyCode < 48 || e.keyCode > 57) e.returnValue = false;
    }
    else {
        if (e.which != 8 && (e.which < 48 || e.which > 57)) e.preventDefault(); // 8 : Back Space
    }
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + '.'+ num.substring(num.length - (4 * i + 3));
    
    return num;
}

function checkEmail(email) {
    if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
        return true;
    }
    else {
        return false;
    }
}
